﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.Site.BackEnd.ApCode.Filters;


namespace GTAuto.Site.BackEnd.Controllers
{

    [AdSection]
    public class AdsController : AdminBaseController
    {
        //
        // GET: /Ads/

        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult Categories()
        {
            int count; 
            List<GTAdCategory> list = DALAdCategory.Singleton.GetPaged(1, 10000, out count);

            return View(list);
        }

        public ActionResult CategoryEdit(int? id)
        {
            GTAdCategory gtAdCategory = null;
            gtAdCategory = id == null ? new GTAdCategory() : DALAdCategory.Singleton.Get((int)id);


            List<GTAdCategory> list = new List<GTAdCategory>();
            GTAdCategory category = new GTAdCategory();
            category.Id = -1;
            category.Name = "Выберите родительскую категорию";
            list.Add(category);
            list.AddRange(DALAdCategory.Singleton.GetMainCategories());
            ViewBag.gtMaincategories = new SelectList(list, "Id", "Name");


            return View(gtAdCategory);
        }
        [HttpPost]
        public ActionResult CategoryEdit(GTAdCategory gtBrand)
        {
            List<GTAdCategory> list = new List<GTAdCategory>();
            GTAdCategory category = new GTAdCategory();
            category.Id = -1;
            category.Name = "Выберите родительскую категорию";
            list.Add(category);
            list.AddRange(DALAdCategory.Singleton.GetMainCategories());
            ViewBag.gtMaincategories = new SelectList(list, "Id", "Name");


            List<string> Error = new List<string>();

            if (string.IsNullOrEmpty(gtBrand.Name))
                Error.Add("Название не может быть пустым");

            if(gtBrand.ParentId!=-1) 
            {
                GTAdCategory gtAdCategory = DALAdCategory.Singleton.Get((int) gtBrand.ParentId);
                if(gtAdCategory.ParentId!=null)
                {
                    Error.Add("Родительская  категория не может иметь свою родителскую категорию");
                }
            }


            if (Error.Count == 0)
            {
                if (gtBrand.ParentId == -1)
                    gtBrand.ParentId = null;

                if (gtBrand.Id > 0)
                {
                    DALAdCategory.Singleton.Update(gtBrand);
                }
                else
                {
                    gtBrand = DALAdCategory.Singleton.Add(gtBrand);
                }
                ViewBag.Succes = "Изменения успешно сохранены";
            }

            ViewBag.Errs = Error;
            return View(gtBrand);
        }

        public ActionResult NewAds(int? id)
        {
            if (id == null)
                id = 1;


            int count; 
            List<GTAd> list = DALAds.Singleton.GetPaged( AdStatusEnum.New, (int) id, Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value), out count);

            ViewBag.TotalCount = count;
            ViewBag.CountForPage = Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value);
            ViewBag.CurrentPage = (int)id;

            return View(list);
        }

        public ActionResult ActiveAds(int? id)
        {
            if (id == null)
                id = 1;


            int count;
            List<GTAd> list = DALAds.Singleton.GetPaged(AdStatusEnum.Active, (int)id, Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value), out count);

            ViewBag.TotalCount = count;
            ViewBag.CountForPage = Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value);
            ViewBag.CurrentPage = (int)id;

            return View(list);
        }

        public ActionResult NoActiveAds(int? id)
        {
            if (id == null)
                id = 1;


            int count;
            List<GTAd> list = DALAds.Singleton.GetPaged(AdStatusEnum.Unactive, (int)id, Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value), out count);

            ViewBag.TotalCount = count;
            ViewBag.CountForPage = Convert.ToInt32(DALSettngs.Singleton.Get("ItemsForPageAdmin").Value);
            ViewBag.CurrentPage = (int)id;

            return View(list);
        }

        public ActionResult AdsView(int id)
        {
            GTAd gtAd = DALAds.Singleton.Get(id);

            var o1 = new SimpleObject {Id = "0", Value = "Новый"};
            var o2 = new SimpleObject { Id = "1", Value = "Активный" };
            var o3 = new SimpleObject { Id = "3", Value = "Неактивный" };
            var list = new List<SimpleObject>();
            list.Add(o1);    
            list.Add(o2);
            list.Add(o3);
            ViewBag.adStats = new SelectList(list, "Id", "Value");
             

            List<GTAdsPicture> gtAdsPictures = DALAdPictures.Singleton.GetByAdsId(id);
            ViewBag.MainPicture =  gtAdsPictures.SingleOrDefault(x => x.Type == (decimal) AdPictureTypeEnum.Main);
            ViewBag.OtherPicrures = gtAdsPictures.Where(x => x.Type == (decimal) AdPictureTypeEnum.Views).ToList();

            return View(gtAd);
        }
        [HttpPost]
        public ActionResult AdsView(GTAd gtAd)
        {

            var Error = new List<string>();
            
            if(string.IsNullOrEmpty(gtAd.Name))
            {
                Error.Add("Название не может быть пустым");
            }

            if (string.IsNullOrEmpty(gtAd.Description))
            {
                Error.Add("Описание не может быть пустым");
            }
            

            if (Error.Count == 0)
            {
                var temp = DALAds.Singleton.Get(gtAd.Id);
                temp.Name = gtAd.Name;
                temp.Description = gtAd.Description;
                temp.Status = gtAd.Status;

                temp.Email = gtAd.Email;
                temp.Site = gtAd.Site;
                temp.Skype = gtAd.Skype;
                temp.telephone1 = gtAd.telephone1;
                temp.telephone2 = gtAd.telephone2;
                temp.telephone3 = gtAd.telephone3;

                temp.Rating = gtAd.Rating;

                DALAds.Singleton.Update(temp);

                ViewBag.Ok = "Ok";
            }
            else
            {
                ViewBag.Errors = Error;
            }

            var o1 = new SimpleObject { Id = "0", Value = "Новый" };
            var o2 = new SimpleObject { Id = "1", Value = "Активный" };
            var o3 = new SimpleObject { Id = "3", Value = "Неактивный" };
            var list = new List<SimpleObject>();
            list.Add(o1);
            list.Add(o2);
            list.Add(o3);
            ViewBag.adStats = new SelectList(list, "Id", "Value");

            List<GTAdsPicture> gtAdsPictures = DALAdPictures.Singleton.GetByAdsId(gtAd.Id);
            ViewBag.MainPicture = gtAdsPictures.SingleOrDefault(x => x.Type == (decimal)AdPictureTypeEnum.Main);
            ViewBag.OtherPicrures = gtAdsPictures.Where(x => x.Type == (decimal)AdPictureTypeEnum.Views).ToList();

            return View(gtAd);
        }

        public ActionResult AdsDelete(int id)
        {
            DALAds.Singleton.Delete(id);
            return RedirectToAction("NewAds");
        }

    }
}
